package com.otdemo.service;


import com.otdemo.dao.RestClassificationDao;
import com.otdemo.dao.RestNoticeDao;
import com.otdemo.dao.RestScoreDao;
import com.otdemo.dao.RestUserDao;
import com.otdemo.entity.RestClassification;
import com.otdemo.entity.RestNotice;
import com.otdemo.entity.RestScore;
import com.otdemo.entity.RestUser;
import com.otdemo.resp.RestNoticeResp;
import com.otdemo.resp.ResultResponse;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class RestNoticeService {

    public List<RestNoticeResp> initList() {
        List<RestNotice> list = new RestNoticeDao().list();
        List<RestUser> userList = new RestUserDao().list();
        List<RestScore> scoreList = new RestScoreDao().list();
        List<RestClassification> classificationList = new RestClassificationDao().list();
        List<RestNoticeResp> respList = new ArrayList<>();
        try {
            int index = 1;
            for (RestNotice rest : list) {
                for (RestClassification restClassification : classificationList) {
                    RestNoticeResp resp = new RestNoticeResp();
                    resp.setName(rest.getName());
                    resp.setId(index);
                    int amount = 0;
                    for (RestUser restUser : userList) {
                        List<RestScore> scoreList1 = scoreList.stream().filter(p -> p.getUser().equals(restUser.getName())
                                && p.getClassification().equals(restClassification.getName())).collect(Collectors.toList());
                        BigDecimal score = new BigDecimal(0.00);
                        for (RestScore restScore : scoreList1) {
                            score = score.add(restScore.getScore());
                        }
                        if (score.compareTo(rest.getScore()) < 0) {
                            continue;
                        }
                        amount += 1;
                    }
                    index++;
                    resp.setClassificationName(restClassification.getName());
                    resp.setAmount(amount);
                    resp.setName(rest.getName());
                    respList.add(resp);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return respList;
    }

    /**
     * 新增
     *
     * @return
     */
    public ResultResponse insert(RestNotice rest) {
        ResultResponse resultResponse = new ResultResponse();
        RestNotice rest1 = new RestNoticeDao().findByUid(rest.getName());
        if (rest1 != null) {
            return resultResponse.fail("名称重复");
        }
        try {
            new RestNoticeDao().insert(rest);
        } catch (Exception e) {
            return resultResponse.fail(e.getMessage());
        }
        return resultResponse.success();
    }

    /**
     * 更新
     *
     * @return
     */
    public ResultResponse update(RestNotice rest) {
        ResultResponse resultResponse = new ResultResponse();
        RestNotice rest1 = new RestNoticeDao().findByUid(rest.getUid());
        if (rest1 == null) {
            return resultResponse.fail("查询不存在");
        }
        try {
            new RestNoticeDao().update(rest);
        } catch (Exception e) {
            return resultResponse.fail(e.getMessage());
        }
        return resultResponse.success();
    }

    public ResultResponse delete(RestNotice rest) {
        ResultResponse resultResponse = new ResultResponse();
        RestNotice rest1 = new RestNoticeDao().findByUid(rest.getName());
        if (rest1 == null) {
            return resultResponse.fail("查询不存在");
        }
        try {
            rest1.setDeleted(Boolean.TRUE);
            new RestNoticeDao().update(rest1);
        } catch (Exception e) {
            return resultResponse.fail(e.getMessage());
        }
        return resultResponse.success();
    }

    /**
     * 查询
     *
     * @return
     */
    public ResultResponse findByUid(RestNotice rest) {
        ResultResponse resultResponse = new ResultResponse();
        try {
            RestNotice rest1 = new RestNoticeDao().findByUid(rest.getUid());
            return resultResponse.success(rest1);
        } catch (Exception e) {
            return resultResponse.fail(e.getMessage());
        }
    }

    public ResultResponse list() {
        ResultResponse resultResponse = new ResultResponse();
        try {
            List<RestNotice> list = new RestNoticeDao().list();
            return resultResponse.success(list);
        } catch (Exception e) {
            return resultResponse.fail(e.getMessage());
        }
    }
}

